// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()

exports.main = async (event, context) => {
  const { type, content, openid, typeName } = event
  if (!content || !type) {
    return { code: 1, msg: '参数不完整' }
  }
  try {
    const res = await db.collection('bottles').add({
      data: {
        type,
        typeName,
        content,
        openid,
        time: new Date(),
        replies: []
      }
    })
    return { code: 0, msg: 'success', id: res._id }
  } catch (e) {
    return { code: 2, msg: '数据库写入失败', error: e }
  }
} 